//
//  ZHTagModel.m
//
// Created by Basic on 15/1/12.
// Copyright (c) 2015 Basic. All rights reserved.
//

#import "HHTagButton.h"
#import <BCFoundation/UIImage+BCHelper.h>
#import <BCFoundation/NSString+BCHelper.h>

@interface HHTagButton()
@property (nonatomic, strong, readwrite) id<HHTagItemProtocol>  tagModel;
@property (nonatomic, weak) HHTagListConfig *config;
@end

@implementation HHTagButton
//MARK: - system
+ (instancetype)hh_allocWithTag:(id<HHTagItemProtocol> )tagModel {
	HHTagButton *btn = [[HHTagButton alloc] init];
    btn.titleLabel.numberOfLines = 0;
    btn.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    btn.tagModel = tagModel;
    [btn setTitle:tagModel.name forState:UIControlStateNormal];
    [btn setAdjustsImageWhenHighlighted:NO];
    return btn;
}
-(void)setSelected:(BOOL)selected {
    [super setSelected:selected];
    if(selected){
        //边框颜色
        if (self.config.itemSelectBorderColor) {
            self.layer.borderColor = self.config.itemSelectBorderColor.CGColor;
            self.layer.borderWidth = self.config.itemBorderWidth;
        } else {
            self.layer.borderWidth = 0;
        }
        //字体
        if (self.config.itemSelectFont) {
            self.titleLabel.font = self.config.itemSelectFont;
        }
        _imgBtn.selected = selected;
    }
    else {
        //边框颜色
        if (self.config.itemBorderColor) {
            self.layer.borderColor = self.config.itemBorderColor.CGColor;
            self.layer.borderWidth = self.config.itemBorderWidth;
        } else {
            self.layer.borderWidth = 0;
        }
        //字体
        self.titleLabel.font = self.config.itemFont;
        _imgBtn.selected = selected;
    }
    self.tagModel.selected = selected;
}
-(CGRect)titleRectForContentRect:(CGRect)contentRect {
    //先判断是否 设置了text frame
    if (self.config && !CGRectEqualToRect(self.config.itemTextFrame, CGRectZero)) {
//        CGSize textSize = CGSizeZerxo;
        //未设置宽度，默认文字大小
        if (self.config.itemTextFrame.size.width<=0 && self.tagModel.name.length>0) {
//            textSize = [NSString bc_autoSize: self.tagModel.name constraint:CGSizeMake(self.config.maxLayoutWidth, 100) font:self.config.itemFont];
//            CGRect tempTextFrame = self.config.itemTextFrame;
//            tempTextFrame.size.width = textSize.width;
//            self.config.itemTextFrame = tempTextFrame;
            
            CGRect tempTextFrame = self.config.itemTextFrame;
            tempTextFrame.size.width = contentRect.size.width - tempTextFrame.origin.x;
            self.config.itemTextFrame = tempTextFrame;
        }
        //未设置高度，默认文字大小
        if (self.config.itemTextFrame.size.height<=0 &&  self.tagModel.name.length>0) {
//            if (CGSizeEqualToSize(textSize, CGSizeZero)) {
//                textSize = [NSString bc_autoSize: self.tagModel.name constraint:CGSizeMake(self.config.maxLayoutWidth, 100) font:self.config.itemFont];
//            }
//            CGRect tempTextFrame = self.config.itemTextFrame;
//            tempTextFrame.size.height = textSize.height;
//            self.config.itemTextFrame = tempTextFrame;
            
            
            CGRect tempTextFrame = self.config.itemTextFrame;
            tempTextFrame.size.height = contentRect.size.height - tempTextFrame.origin.y;
            self.config.itemTextFrame = tempTextFrame;
        }
        return self.config.itemTextFrame;
    } else {
        return [super titleRectForContentRect:contentRect];
    }
}
-(CGRect)imageRectForContentRect:(CGRect)contentRect {
    if (self.config && !CGRectEqualToRect(self.config.itemImgFrame, CGRectZero)) {
        //未设置宽度，默认文字大小
        if (self.config.itemImgFrame.size.width<=0 && self.config.itemImg) {
            CGRect tempImgFrame = self.config.itemImgFrame;
            tempImgFrame.size.width = self.config.itemImg.size.width;
            self.config.itemImgFrame = tempImgFrame;
        }
        //未设置高度，默认文字大小
        if (self.config.itemImgFrame.size.height<=0 && self.config.itemImg) {
            CGRect tempImgFrame = self.config.itemImgFrame;
            tempImgFrame.size.height = self.config.itemImg.size.height;
            self.config.itemImgFrame = tempImgFrame;
        }
        return self.config.itemImgFrame;
    } else {
        return [super imageRectForContentRect:contentRect];;
    }
}

//MARK: - getter
-(void)setImgBtn:(UIButton *)imgBtn {
    if (_imgBtn) {
        [_imgBtn removeFromSuperview];
    }
    _imgBtn = imgBtn;
    [self addSubview:_imgBtn];
}

//MARK: - public
- (void)hh_loadWithConfig:(HHTagListConfig *)config {
    _config = config;
    if (config.itemCornerRadius>0) {
        self.layer.cornerRadius = config.itemCornerRadius;
        self.layer.masksToBounds = YES;
    }
    self.titleLabel.font = config.itemFont;
    if (config.textNumberOfLines > 0) {
        self.titleLabel.numberOfLines = config.textNumberOfLines;
    }
    if (config.fixedItemWidth < 0.1) {
        self.contentEdgeInsets = config.itemPadding;
    }
    //正常状态
    [self setTitleColor:config.itemTextColor forState:UIControlStateNormal];
    [self setBackgroundImage:[UIImage bc_imageWithColor:config.itemBgColor] forState:UIControlStateNormal];
    if (config.itemBorderWidth>0 && config.itemBorderColor) {
        self.layer.borderColor = config.itemBorderColor.CGColor;
        self.layer.borderWidth = config.itemBorderWidth;
    }
    if (config.itemImg) {
        [self setImage:config.itemImg forState:UIControlStateNormal];
        if (!config.isItemImgRight) {
            //image在左侧
            if (config.itemImgTextSpace>0) {
                self.imageEdgeInsets = UIEdgeInsetsMake(0, -config.itemImgTextSpace, 0, config.itemImgTextSpace);
            }
        }
        else {
            //image在右侧
            CGFloat titleOffset = config.itemImgTextSpace/2.0 + config.itemImg.size.width;
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -titleOffset, 0, titleOffset);
            NSString *titleStr = [self titleForState:UIControlStateNormal];
            CGFloat imgOffset = config.itemImgTextSpace/2.0 + [NSString bc_autoWidth:titleStr stringFont:self.titleLabel.font];
            self.imageEdgeInsets = UIEdgeInsetsMake(0, imgOffset, 0, -imgOffset);
        }
    }
    //选中状态
    if (config.itemSelectTextColor) {
        [self setTitleColor:config.itemSelectTextColor forState:UIControlStateSelected];
    }
    if (config.itemSelectBgColor) {
        [self setBackgroundImage:[UIImage bc_imageWithColor:config.itemSelectBgColor] forState:UIControlStateSelected];
    }
    if (config.itemSelectImg) {
        [self setImage:config.itemSelectImg forState:UIControlStateSelected];
    }
}

//MARK: - 计算size
+(CGSize )intrinsicContentSizeWithTag:(id<HHTagItemProtocol> )tagModel withConfig:(HHTagListConfig *)config
{
    return [[self class] intrinsicContentSize:tagModel.name withConfig:config];
}
+(CGSize )intrinsicContentSize:(NSString *)text withConfig:(HHTagListConfig *)config
{
    CGSize contentSize = CGSizeZero;
    //计算文本大小
    CGFloat lineLeft = config.linePadding.left + config.contentPadding.left;
    CGFloat lineRight = config.linePadding.right + config.contentPadding.right;
    CGFloat contentMaxWidth = config.maxLayoutWidth - (config.itemPadding.left + config.itemPadding.right) - (lineLeft+lineRight);
    contentSize = [NSString bc_autoSize:text constraint:CGSizeMake(contentMaxWidth, MAXFLOAT) font:config.itemFont];
    //计算高度
    if (config.lineHeight>0) {
        //如果固定了高度
        contentSize.height = config.lineHeight;
    } else {
        //如果没有固定高度，加上 item 上下空白
        contentSize.height += config.itemPadding.top + config.itemPadding.bottom+1;
    }
    //计算宽度
    if (config.fixedItemWidth>0) {
        //如果这里是固定宽度
        contentSize.width = config.fixedItemWidth;
    } else {
        //如果没有固定宽度
        if (config.itemImg) {
            //有图片，实际宽度=图片宽度+间距+文字宽度
            contentSize.width += config.itemImg.size.width + config.itemImgTextSpace;
        }
        //加上 item 左右空白
        contentSize.width += config.itemPadding.left + config.itemPadding.right;
    }
    return contentSize;
}
@end
